package freecomm.study.simpleexplorer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity
{
    /** Called when the activity is first created. */
    private List<String> itemts = null;
    private List<String> paths = null;
    private static final String ROOT_PATH_STRING = "/";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getFileDir(ROOT_PATH_STRING);
    }

    private void getFileDir(String filePath)
    {
        setTitle(filePath);
        itemts = new ArrayList<String>();
        paths = new ArrayList<String>();
        File f = new File(filePath);
        File[] files = f.listFiles();
        if (!filePath.equals(ROOT_PATH_STRING))
        {
            itemts.add("Back to " + ROOT_PATH_STRING);
            paths.add(ROOT_PATH_STRING);
        }
        for (int i = 0; i < files.length; i++)
        {
            File file = files[i];
            itemts.add(file.getName());
            paths.add(file.getPath());
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, itemts);
        setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView content, View v, int position, long id)
    {
        File file = new File(paths.get(position));
        if (file.isDirectory())
        {
            getFileDir(paths.get(position));
        } else
        {
            new AlertDialog.Builder(this).setIcon(R.drawable.icon)
                    .setTitle("(" + file.getName() + ")" + " is FILE!")
                    .setPositiveButton("OK", new DialogInterface.OnClickListener()
                    {

                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub

                        }
                    }).show();
        }
    }

}